/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_share.service.impl;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.yuecai.modules.yc_share.domain.YcShare;
import com.yuecai.modules.yc_share.repository.YcShareRepository;
import com.yuecai.modules.yc_share.service.dto.YcShareDto;
import com.yuecai.modules.yc_share.service.dto.YcShareQueryCriteria;
import com.yuecai.modules.yc_share.service.mapstruct.YcShareMapper;
import com.yuecai.utils.FileUtil;
import com.yuecai.utils.PageUtil;
import com.yuecai.utils.QueryHelp;
import com.yuecai.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author dm
* @date 2020-08-24
**/
@Service
@RequiredArgsConstructor
public class YcShareService {

    private final YcShareRepository ycShareRepository;
    private final YcShareMapper ycShareMapper;

    
    public Map<String,Object> queryAll(YcShareQueryCriteria criteria, Pageable pageable){
        Page<YcShare> page = ycShareRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ycShareMapper::toDto));
    }

    
    public List<YcShareDto> queryAll(YcShareQueryCriteria criteria){
        return ycShareMapper.toDto(ycShareRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    
    @Transactional
    public YcShareDto findById(Long id) {
        YcShare ycShare = ycShareRepository.findById(id).orElseGet(YcShare::new);
        ValidationUtil.isNull(ycShare.getId(),"YcShare","id",id);
        return ycShareMapper.toDto(ycShare);
    }

    
    @Transactional(rollbackFor = Exception.class)
    public YcShareDto create(YcShare resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return ycShareMapper.toDto(ycShareRepository.save(resources));
    }

    
    @Transactional(rollbackFor = Exception.class)
    public void update(YcShare resources) {
        YcShare ycShare = ycShareRepository.findById(resources.getId()).orElseGet(YcShare::new);
        ValidationUtil.isNull( ycShare.getId(),"YcShare","id",resources.getId());
        ycShare.copy(resources);
        ycShareRepository.save(ycShare);
    }

    
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            ycShareRepository.deleteById(id);
        }
    }

    
    public void download(List<YcShareDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YcShareDto ycShare : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题", ycShare.getTitle());
            map.put("图片", ycShare.getImage());
            map.put("内容", ycShare.getContent());
            map.put("分享类型，0app,1商品", ycShare.getShareType());
            map.put("分享URL", ycShare.getShareUrl());
            map.put("是否删除", ycShare.getDel());
            map.put("创建时间", ycShare.getCreateTime());
            map.put("过期时间", ycShare.getExpirationTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}